<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="__static__/admin/sku2/css/sku_style.css" />

    <script type="text/javascript" src="__static__/admin/sku2/js/jquery.min.js"></script>
    <script type="text/javascript" src="__static__/admin/sku2/js/createSkuTable.js"></script>
    <script type="text/javascript" src="__static__/admin/sku2/js/customSku.js"></script>
    <script type="text/javascript" src="__static__/admin/sku2/plugins/layer/layer.js"></script>
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-form-admin" id="layuiadmin-form-admin" style="padding:10px 30px;">
    <div style="width: 80%;margin: 10px auto;">
        <span style="width:200px;height:80px;float: left;text-align: center;"><img src="{$list.thumb}" style="height: 80px;"></span>
        <span style="width: 300px;height:80px;line-height:120px;text-align: left">{$list.name}</span>
        <input value="{$list.id}" type="hidden" id="goods_id">
    </div>
    <!--start  attr_type -->
    <div id="selected" style="width: 100%;text-align: center;">
    <select name="attr_id" id="attr_type" style="width: 80%;height: 40px;line-height: 40px;border: 1px solid #ccc;">
    <option value="0">--规格类型--</option>
    {volist name="attr_cate" id="v"}
    {if condition="isset($list.attr_id)&&$v.id eq $list.attr_id "}
    <option value="{$v.id}" selected>{$v.name}</option>
    {else /}
    <option value="{$v.id}">{$v.name} </option>
    {/if}
    {/volist}
    </select>
        <p style="color:red;">规格类型：选择则商品多属性，不选择则商品单属性</p>
    </div>
    <!--end-->
    <div id="summaryContent" style="padding: 0px 15px;">

    </div><!---sku动态框-->

<div id="skuTable">
    <table class="skuTable">
        <tbody>
        <tr>
            {if condition="isset($type_name)"}
            {volist name="type_name" id="v"}
            <th>{$v.name}</th>
            {/volist}
            {/if}
            <th>市场价/原价</th>
            <th>销售价</th>
            <th>库存</th>
        </tr>
        {if condition="isset($spec)"}
        {volist name="spec" id="v"}
        <tr class="sku_table_tr"  propvalids="{$v.arrt_id}">
            {volist name="v.title" id="vs"}
            <td>{$vs}</td>
            {/volist}
            <td><input type="text" class="setting_sell_price" value="{$v.original_price}"></td>
            <td><input type="text" class="setting_sku_price" value="{$v.sell_price}"></td>
            <td><input type="text" class="setting_sku_stock" value="{$v.stock}">
                <input type="hidden" value="{$v.arrt_id}" name="attr_arr[]"/>
            </td>
        </tr>
        {/volist}
        {else}
        <tr>
            <td><input type="text" class="setting_sell_price" value=""></td>
            <td><input type="text" class="setting_sku_price" value=""></td>
            <td><input type="text" class="setting_sku_stock" value="">
            </td>
        </tr>
        {/if}
        </tbody>
    </table>
</div>
<div style="width: 80%;text-align: right;padding: 10px;">

    <input type="button" value="提交" onclick="test();" style="width: 80px;height: 30px;background: #1E9FFF;color: white;border: #1E9FFF;cursor:pointer;border-radius: 5px"/>
</div>
</div>

<script type="text/javascript" src="__static__/admin/sku2/js/getSetSkuVals.js"></script>
<script>


    // $(document).ready(function(){
    //
    // });
    window.onload=function(){
        $("#attr_type").change();
       }

    $("#attr_type").change(function(){

        var opt=$("#attr_type").val();
        var goods_id=$("#goods_id").val();
       // alert(opt);
        $.ajax({
            url:"{:url('goods.Goods/get_attr')}" ,
            type: "post",
            dataType : "json",
            data: {
                'type':opt,
                'goods_id':goods_id,
            },
            success: function(e){
                // console.log(e,'==================eeeessss')
                if(e.code == 200){
                    //属性和已选择的属性
                    if(e.result.attr_arry){
                        var data =  e.result.attr_arry;

                        var tableContent = " ";
                        for(var i=0;i < data.length ;i++){
                            var value =data[i].value; ;
                            tableContent += '<ul class="SKU_TYPE">';
                            tableContent += '<li is_required="1" propid="'+data[i].id+'" sku-type-name="'+data[i].key+'" style="font-weight: bold">'+data[i].key+':</li>';

                            tableContent += ' </ul><ul>';
                            for(var j=0;j < value.length ;j++) {
                                if(value[j].checked == 1 ){
                                    //已选择的属性
                                    tableContent += ' <li><label><input type="checkbox" class="sku_value" propvalid="' + value[j].id + '" value="' + value[j].name + '" checked />' + value[j].name + '</label></li>';

                                    getAlreadySetSkuVals();

                                }else {
                                    tableContent += ' <li><label><input type="checkbox" class="sku_value" propvalid="' + value[j].id + '" value="' + value[j].name + '" />' + value[j].name + '</label></li>';
                                }

                            }
                            tableContent += ' </ul><div class="clear"></div>';
                        }
                        //将拼接的页面放入列表里面
                        $("#summaryContent").html(tableContent);
                    }else{

                    }
                 //end
                    //已保存的商品规格信息
                    if(e.result.goods_attr){
                        console.log(e.result.goods_attr,'e.result.goods_attr');
                        var goods_attr= e.result.goods_attr;
                        for(var k=0;k < goods_attr.length ;k++){
                            alreadySetSkuVals[goods_attr[k].arrt_id] = {
                                "attr_arr":goods_attr[k].arrt_id,
                                "skuPrice" : goods_attr[k].sell_price,
                                "SellPrice" : goods_attr[k].original_price,
                                "skuStock" : goods_attr[k].stock
                            }
                        }
                    }
                    //end
                    console.log(alreadySetSkuVals,'=======================>544alreadySetSkuVals<')
                }
            },
            error: function(){

            }
        });
    });

    //提交数据
    function test() {
    //    $("#sku_value").change();
       getAlreadySetSkuVals();//获取已经设置的SKU值
        console.log(alreadySetSkuVals,'===================已设置的属性1 alreadySetSkuVals');

        var goods_id =$('#goods_id').val();
        var attr_type = $('#attr_type').val();
        $.ajax({
            url:"goods_attr" ,
            type: "post",
            dataType : "json",
            data: {
                'goods_id':goods_id,
                'type':attr_type,
                'value':alreadySetSkuVals,
            },
            success: function(e){
                console.log(e,'==================eeee')
                if(e.code == 200){
                    window.parent.location.reload();
                }
            },
            error: function(){

            }
        });
    }
</script>
</body>
</html>
